﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Controls_wucDatePicker : System.Web.UI.UserControl
{
    private string[] months = { "Month", "January", "February", "March", "April", "May", "June", 
                                  "July", "August", "September", "October", "Novmember", "December" };
    private int _yearFrom;
    public int YearFrom
    {
        get { return _yearFrom; }
        set { _yearFrom = value; }
    }

    private int _yearTo;
    public int YearTo
    {
        get { return _yearTo; }
        set { _yearTo = value; }
    }

    public DateTime Date
    {
        get
        {
            if (ddlDay.SelectedValue != "-1" && ddlMonth.SelectedValue != "-1" && ddlYear.SelectedValue != "-1")
                return new DateTime(int.Parse(ddlYear.SelectedValue),
                    int.Parse(ddlMonth.SelectedValue), int.Parse(ddlDay.SelectedValue),0,0,1);
            else
                return DateTime.MinValue;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitControls();
        }
    }

    private void InitControls()
    {
        if (YearFrom < 1800 || YearFrom > 2100)
            throw new ArgumentOutOfRangeException("Year from can't be less than 1800 or greater than 2100");
        if (YearTo < 1800 || YearFrom > 2100)
            throw new ArgumentOutOfRangeException("Year to can't be less than 1800 or greater than 2100");
        if (YearTo - YearFrom > 20)
            throw new ArgumentOutOfRangeException("Total year difference can't be more than 20");

        ddlYear.Items.Clear();
        ddlYear.Items.Add(new ListItem("Year", "-1"));

        for (int i = YearFrom; i <= YearTo; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }

        ddlMonth.Items.Clear();
        for (int i = 0; i < months.Length; i++)
        {
            if (i == 0)
                ddlMonth.Items.Add(new ListItem(months[i], "-1"));
            else
                ddlMonth.Items.Add(new ListItem(months[i], i.ToString()));
        }

        AddDays(31);
    }

    private void AddDays(int days)
    {
        int selectedDay = -1;
        if(ddlDay.Items.Count > 0)
            selectedDay = int.Parse(ddlDay.SelectedValue);

        ddlDay.Items.Clear();
        ddlDay.Items.Add(new ListItem("Day", "-1"));
        for (int i = 1; i <= days; i++)
        {
            ddlDay.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
        if (selectedDay <= days)
            ddlDay.SelectedValue = selectedDay.ToString();
        else
            ddlDay.SelectedValue = days.ToString();
    }

    protected void Month_Changed(object sender, EventArgs e)
    {
        if (ddlYear.SelectedValue != "-1" && ddlMonth.SelectedValue != "-1")
            AddDays(DateTime.DaysInMonth(int.Parse(ddlYear.SelectedValue),int.Parse(ddlMonth.SelectedValue)));
    }

    protected void Day_Changed(object sender, EventArgs e)
    {

    }

    protected void Year_Changed(object sender, EventArgs e)
    {
        if(ddlYear.SelectedValue != "-1" && ddlMonth.SelectedValue != "-1")
            AddDays(DateTime.DaysInMonth(int.Parse(ddlYear.SelectedValue), int.Parse(ddlMonth.SelectedValue)));
    }
}
